package com.opgea.crm.domain.entities;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import com.opgea.crm.domain.entities.security.CrmUser;


@Entity
@Table(name="projectdetails")
public class ProjectDetails implements Serializable {

	

	@Id
	@GeneratedValue
	@Column(name="id")
	private Long id;
	
	@Column(name="projectName")
	private String projectName;
	
	@ManyToOne
    @JoinColumn(name = "status")
	private SetupsValue status;
	
	@ManyToOne
    @JoinColumn(name = "priority")
	private SetupsValue priority;
	
	@Column(name="actualStartDate")
	@Temporal(TemporalType.DATE)
	private Date actualStartDate;
	
	@Column(name="actualEndDate")
	@Temporal(TemporalType.DATE)
	private Date actualEndDate;
	
	@Column(name="actualBudget")
	private Double actualBudget;
	
	@Column(name="actualTakenHrs")
	private Integer actualTakenHrs;
	@Lob
	@Column(name="projectDetails")
	private byte[] projectDetails;
	
	@JoinColumn(name="customer_id", referencedColumnName="id")
	@ManyToOne
	private Lead customer;
	
	@JoinColumn(name="additionalcontact_id", referencedColumnName="id")
	@ManyToOne
	private AdditionalContact contactTo;
	
	@OneToMany(cascade=CascadeType.ALL, mappedBy="projectId")
	private List<Task> tasks;
	
	
	@OneToMany(cascade=CascadeType.ALL, mappedBy="project" , fetch = FetchType.LAZY)
	private List<Issue> issues;
	
	
	@OneToMany(cascade=CascadeType.ALL, mappedBy="project")
	private List<Engagement> engagements;
	
	
	@OneToMany(cascade=CascadeType.ALL, mappedBy="project")
	private List<ProjectDocuments> documents;
	
	
	@OneToMany(cascade=CascadeType.ALL, mappedBy="project")
	private List<ProjectEstimationHistory> estimations;

	@ManyToMany(fetch = FetchType.LAZY)
	private List<Employee> members;	
	
	@JoinColumn(name="createdBy", referencedColumnName="username")
	@ManyToOne
	private CrmUser createdBy;
	
	@Column(name="createdAt")
	@Temporal(TemporalType.DATE)
	private Date createdAt;
	
	
	
	public ProjectDetails() {
		super();
	//	members = new ArrayList<Employee>();
	}
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getProjectName() {
		return projectName;
	}
	public void setProjectName(String projectName) {
		this.projectName = projectName;
	}
	public SetupsValue getStatus() {
		return status;
	}
	public void setStatus(SetupsValue status) {
		this.status = status;
	}
	public SetupsValue getPriority() {
		return priority;
	}
	public void setPriority(SetupsValue priority) {
		this.priority = priority;
	}
	public Date getActualStartDate() {
		return actualStartDate;
	}
	public void setActualStartDate(Date actualStartDate) {
		this.actualStartDate = actualStartDate;
	}
	public Date getActualEndDate() {
		return actualEndDate;
	}
	public void setActualEndDate(Date actualEndDate) {
		this.actualEndDate = actualEndDate;
	}
	public Double getActualBudget() {
		return actualBudget;
	}
	public void setActualBudget(Double actualBudget) {
		this.actualBudget = actualBudget;
	}
	public Integer getActualTakenHrs() {
		return actualTakenHrs;
	}
	public void setActualTakenHrs(Integer actualTakenHrs) {
		this.actualTakenHrs = actualTakenHrs;
	}
	public byte[] getProjectDetails() {
		return projectDetails;
	}
	public void setProjectDetails(byte[] projectDetails) {
		this.projectDetails = projectDetails;
	}
	public Lead getCustomer() {
		return customer;
	}
	public void setCustomer(Lead customer) {
		this.customer = customer;
	}
	public void setContactTo(AdditionalContact contactTo) {
		this.contactTo = contactTo;
	}
	public AdditionalContact getContactTo() {
		return contactTo;
	}
	public List<Employee> getMembers() {
		return members;
	}
	public void setMembers(List<Employee> members) {
		this.members = members;
	}
	public List<Task> getTasks() {
		return tasks;
	}
	public void setTasks(List<Task> tasks) {
		this.tasks = tasks;
	}
	public List<Issue> getIssues() {
		return issues;
	}
	public void setIssues(List<Issue> issues) {
		this.issues = issues;
	}
	public List<Engagement> getEngagements() {
		return engagements;
	}
	public void setEngagements(List<Engagement> engagements) {
		this.engagements = engagements;
	}
	public List<ProjectDocuments> getDocuments() {
		return documents;
	}
	public void setDocuments(List<ProjectDocuments> documents) {
		this.documents = documents;
	}
	public CrmUser getCreatedBy() {
		return createdBy;
	}
	public void setCreatedBy(CrmUser createdBy) {
		this.createdBy = createdBy;
	}
	public Date getCreatedAt() {
		return createdAt;
	}
	public void setCreatedAt(Date createdAt) {
		this.createdAt = createdAt;
	}
	public void setEstimations(List<ProjectEstimationHistory> estimations) {
		this.estimations = estimations;
	}
	public List<ProjectEstimationHistory> getEstimations() {
		return estimations;
	}
	
	public int getNoOfLowPriorityIssue(String priority)
	{
		int count = 0;
		for(Issue data : this.issues){
			if(data.getPriority().getFieldValue().equalsIgnoreCase(priority))count=count+1;
		}
		return count;
	}
	
	public ProjectDetails(Long id, String projectName){//, Employee members) {
		
		this.id = id;
		this.projectName = projectName;
		//this.members.add(members);
	}
}
